//
//  HODepartmentViewController.m
//  微度报修
//
//  Created by foguo-mac-1 on 6/15/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HODepartmentViewController.h"
#import "HODepartmentEntity.h"
#import "HOConstants.h"
#import "HOStaticDataMgr.h"

@interface HODepartmentViewController ()

@end

@implementation HODepartmentViewController
@synthesize delegate;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	
	[self initTableView];
	[self initDataSource];
	[self requestDataWithMaxId:1];
}

- (void)initDataSource
{
}

- (void)viewWillAppear:(BOOL)animated
{
	self.tabBarController.tabBar.hidden = YES;
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:NSLocalizedString(@"选择负责部门", @"选择负责部门")];
//	[self requestDataWithMaxId:0];
}

- (void)requestDataWithMaxId:(int)maxId
{
	if([self.tableViewDataList count] == 0)
		[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_Department,
								  kHO_FIELD_MaxID:[NSNumber numberWithInt:maxId]}];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return CELL_HEIGHT;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	NSInteger count = [self.tableViewDataList count];
	[self updateNoResultView:count];;
	return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *AMBaseTableViewCellIdentifier = @"HOTableViewCell";
    HOTableViewCell *cell = (HOTableViewCell *)[tableView dequeueReusableCellWithIdentifier:AMBaseTableViewCellIdentifier];
    if(cell == nil)
    {
        cell = [[[HOTableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier: AMBaseTableViewCellIdentifier] autorelease];
    }
	HODepartmentEntity *entity = [self.tableViewDataList objectAtIndex:indexPath.row];
	cell.textLabel.text = entity.departmentName;
	cell.textLabel.font = [UIFont systemFontOfSize:15];
	cell.textLabel.textColor = TEXT_COLOR_181818;
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	cell.selectionStyle = UITableViewCellSelectionStyleGray;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
	
	HODepartmentEntity *entity = [self.tableViewDataList objectAtIndex:indexPath.row];
	[self goBack];
	[self.delegate didSelectDepartment:entity];
    
}
#pragma mark

- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_Department])
	{
		if(![response.dataNode isKindOfClass:[NSArray class]])
		{
			return;
		}
		NSArray *array = response.dataNode;
		[self.tableViewDataList removeAllObjects];
		for(NSInteger i = 0; i < [array count];i ++)
		{
			NSDictionary *dic = [array objectAtIndex:i];
			int entityId = [[dic objectForKey:kHO_FIELD_DepartmentId] intValue];
			HODepartmentEntity *entity = [[HOStaticDataMgr sharedInstance] getDepartmentEntityWithId:entityId];
			[entity parseDic:dic];
			if(self.exceptDepartmentIds)
			{
				if(![self.exceptDepartmentIds containsObject:[NSNumber numberWithInt:entity.departmentId]])
				{
					[self.tableViewDataList addObject:entity];
				}
			}
			else
			{
				[self.tableViewDataList addObject:entity];
			}

		}
		[baseTableview reloadData];
	}
}


- (void)dealloc
{
	[self.exceptDepartmentIds removeAllObjects];
	self.exceptDepartmentIds = nil;
	self.delegate = nil;
	[super dealloc];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
